package com.xh.bussiness.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.xh.bussiness.system.bean.dto.ParentBean;
import com.xh.bussiness.system.bean.dto.ParentStudentBean;
import com.xh.bussiness.system.bean.request.ParentRequest;
import com.xh.bussiness.system.service.IParentService;
import com.xh.bussiness.system.service.IParentStudentService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Pitt zhang
 * @since 2019-04-09
 */
@Controller
@RequestMapping("/system/parent")
public class ParentController extends BaseController {

    @Autowired
    private IParentService parentService;
    @Autowired
    private IParentStudentService parentStudentService;

    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("/toList")
    public String toList(){
        return "admin/system/parent/list";
    }


    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/queryPage")
    @ResponseBody
    public XhPage queryPage(XhPage page, ParentRequest bean){
        page=parentService.queryParentPage(page,bean);
        return page;
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @RequestMapping(value="/exportAll",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult exportAll(ParentRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=parentService.exportAll(request,exportHeaderBeans,bean,"queryParentPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 前往维护页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "admin/system/parent/add";
    }

    /**
     * 前往维护页面
     * @return
     */
    @RequestMapping("/toModify/{id}")
    public String toModify(@PathVariable String id){
        return "admin/system/parent/modify";
    }

    /**
     * 前往查看页面
     * @param id
     * @return
     */
    @RequestMapping("/toView/{id}")
    public String toView(@PathVariable String id){
        System.out.println(JSONObject.toJSON(parentService.getById(id)));
        return "admin/system/parent/view";
     }

    /**
     * 新增和保存
     * @param parentBean 实体对象
     * @return
     */
    @RequestMapping(value="/saveOrUpdate",method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public AjaxResult saveOrUpdate(ParentBean parentBean){
        if(parentBean.getId()==null){
            parentService.insertObj(parentBean);
        }else{
            parentService.updateObj(parentBean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @RequestMapping("/queryById/{id}")
    @ResponseBody
    public AjaxResult queryById(@PathVariable String id){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, parentService.getById(id));
    }


    @RequestMapping("/queryByUserId/{userId}")
    @ResponseBody
    public AjaxResult queryByUserId(@PathVariable String userId){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, parentService.getParentByUserId(userId));
    }
    /**
     * 删除操作
     * @param id
     * @return
     */
    @RequestMapping("/deleteById/{id}")
    @ResponseBody
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        parentService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        parentService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation(value = " 家长绑定学生")
    @RequestMapping("/saveBindParentStudent")
    @ResponseBody
    @Transactional
    public AjaxResult saveBindParentStudent(ParentStudentBean parentStudentBean) {
        parentStudentService.insertObj(parentStudentBean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

